package com.sojpt.boot.controller.business;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyTeacherReleaseRecordAddValidator;
import com.sojpt.boot.controller.business.validator.LkyTeacherReleaseRecordUpdateValidator;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyTableStorage;
import com.sojpt.model.LkyTeacherReleaseRecord;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Sohnny
 */
@RestController
@RequestMapping("/admin/record/")
//@Before({AdminLoginInterceptor.class, AdminPopedomInterceptor.class})
public class LkyTeacherReleaseRecordAdminController extends IViewController {

    /*
     * 查看老师管理下的班级
     * */
    @GetMapping("list")
    public void list() {
        Map<String, String> map = new HashMap<>();
        map.put("tid", getPara("tid"));
        map.put("cid", getPara("cid"));
        map.put("mid", getPara("mid"));
        map.put("coid", getPara("coid"));
        SqlPara sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.selectClassByTid", map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @Before(LkyTeacherReleaseRecordAddValidator.class)
    @PostMapping("save")
    public void save() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/record/save");
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord = getModel(LkyTeacherReleaseRecord.class, "lkyTeacherReleaseRecord");
        lkyTeacherReleaseRecord.setId(UUID.randomUUID().toString().replace("-", ""));
        if (lkyTeacherReleaseRecord.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("添加失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyTeacherReleaseRecordUpdateValidator.class)
    @PostMapping("update")
    public void update() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/record/update");
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord = getModel(LkyTeacherReleaseRecord.class, "lkyTeacherReleaseRecord");
        if (lkyTeacherReleaseRecord.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("更新成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("更新失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyTeacherReleaseRecordUpdateValidator.class)
    @PostMapping("updateT")
    public void updateT() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/record/updateT");
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord = getModel(LkyTeacherReleaseRecord.class, "lkyTeacherReleaseRecord");
        String tid1 = lkyTeacherReleaseRecord.getTid();
        String tid = Db.queryStr("SELECT lt.id FROM dic_user du, lky_teacher lt WHERE du.ID =? and du.USER_CODE = lt.job_number", tid1);
        lkyTeacherReleaseRecord.setTid(tid);
        if (lkyTeacherReleaseRecord.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("更新成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("更新失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }


    @PostMapping("delete")
    public void delete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/record/delete");
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord = getModel(LkyTeacherReleaseRecord.class, "lkyTeacherReleaseRecord");
        if (LkyTeacherReleaseRecord.dao.deleteById(lkyTeacherReleaseRecord.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if (Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    //查询班级下能发布的任务
    @PostMapping("releaseRecordList")
    public void releaseRecordList() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", getPara("uid"));
        map.put("cid", getPara("cid"));
        System.out.println("进入releaseRecordList方法");
        SqlPara sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.releaseRecordList", map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }


    //查询老师发布班级任务集合
    @PostMapping("teacherReleaseRecordList")
    public void teacherReleaseRecordList() {
        Map<String,String> map =new HashMap<>();
        String uid = getPara("uid");
        String roid=Db.queryStr("SELECT `ROLE_ID` FROM `dic_user_role` WHERE `USER_ID`=?",uid);
        map.put("uid",uid);
        map.put("cid",getPara("cid"));
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("dtmId",getPara("dtmId"));
        SqlPara sqlPara = null;
        if (roid.equals("7371f2ced4154b8aaab16cccf378e7c3")) {     //老师权限
            sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.teacherReleaseRecordList", map);
        }else if(roid.equals("b40d665874274e399985083485a42785"))  {      //总管理
            sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.adminSeeTeacherReleaseRecordList", map);
        }else if (roid.equals("a6a1564fdea64acc975266566dc61607")) {         //学院权限
            String cid=Db.queryStr(" SELECT le.id FROM `dic_user` dr,`lky_college` le WHERE dr.USER_CODE=le.collage_number AND dr.ID=?",uid);
            map.put("cid1",cid);
            sqlPara = Db.getSqlPara("lky_teacherReleaseRecord.teachercoller", map);
        }
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    //老师发布记录——删除功能
    @PostMapping("teacherReleaseRecordDel")
    public void teacherReleaseRecordDel() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/record/teacherReleaseRecordDel");
        String recordId = getPara("recordId");
        String tsId = Db.queryStr("select ltrr.fid from lky_teacher_release_record ltrr where ltrr.id=?", recordId);
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord = new LkyTeacherReleaseRecord();
        lkyTeacherReleaseRecord.setId(recordId);
        LkyTableStorage lkyTableStorage = new LkyTableStorage();
        lkyTableStorage.setId(tsId);
        if (LkyTeacherReleaseRecord.dao.deleteById(lkyTeacherReleaseRecord.getId()) && LkyTableStorage.dao.deleteById(lkyTableStorage.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }
}